package com.zggk.framework.service.score;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.bean.Message;
import com.platform.common.utils.JsonUtils;
import com.platform.example.entity.Person;
import com.platform.framework.service.BaseServiceImpl;
import com.zggk.framework.dao.score.GjContenthighRepository;
import com.zggk.framework.entity.score.GjContentcomm;
import com.zggk.framework.entity.score.GjContenthigh;

/**
 * @Description 
 * @author 张立增[zhanglizeng] Tel：18860126570
 * @email 1472052711@qq.com
 * @date 2020-02-26 11:30:46
 */
 @Service
public class GjContenthighService extends BaseServiceImpl {

    @Autowired
    GjContenthighRepository gjContenthighRepository;
 
	/**
	 * @Description 得到列表数据
	 * @author 张立增
	 * @Date 2020年2月26日 下午9:44:26
	 */
	public Page<GjContenthigh> getDataGridBySearchContent(String searchKey, Pageable pageable) {
		try {
			String hql = "SELECT t1.*,CASE WHEN EXISTS(SELECT 1 FROM score.gj_contenthigh t2 WHERE t1.id = t2.pid) THEN '0' ELSE '1' END  AS nodeFlag " + 
					"FROM score.gj_contenthigh t1 ";
			List<String> params = new ArrayList<String>();
			if (!StringUtils.isEmpty(searchKey)) {
				hql += " where t1.project_prefs  like ? ";
				params.add("%"+searchKey+"%");     
			}
			return super.getDao().findEntityPagingBySql(hql, params.toArray(), pageable, GjContenthigh.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Description 得到父类编号
	 * @author 张立增
	 * @Date 2020年2月27日 下午2:41:52
	 */
	public String getPidTree() {
		try {
			String hql = "SELECT 0 AS id,-1 AS pid UNION SELECT id,pid FROM score.gj_contenthigh ORDER BY id,pid ASC ";
			List<GjContenthigh> list = super.getDao().findBySql(hql,GjContenthigh.class);
			return JsonUtils.dealJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

    /**
     * @Description 检查id是否存在的方法
     * @author 张立增
     * @Date 2020年2月27日 下午4:42:37
     */
	public String checkIdExist(String id) {
	    Message message =  new Message();
		try {
			String hql = "SELECT id from score.gj_contenthigh where id = ? ";
			List<GjContenthigh> list = super.getDao().findBySql(hql,new Object[] {id},GjContenthigh.class);
			if(null == list || list.size() == 0) {
				message.setType(200);
			}else {
				message.setType(400);
				message.setMsg("编码重复");
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setType(400);
			message.setMsg("校验错误");
		}
		return message.toJson();
	}

    /**
     * @Description 添加数据
     * @author 张立增
     * @Date 2020年2月28日 下午12:38:33
     */
	public String dataAdd(GjContenthigh gjContenthigh) {
		try {
			gjContenthighRepository.save(gjContenthigh);
		} catch (Exception e) {
			return new Message(Message.ERROR, "添加评分标准失败").toJson();
		}
		return new Message(Message.OK, gjContenthigh.getId()).toJson();
	}

    /**
     * @Description 更新数据
     * @author 张立增
     * @Date 2020年2月28日 下午12:38:33
     */
	public String dataUpdate(GjContenthigh gjContenthigh) {
		try {
			String id = gjContenthigh.getId();         //编号
		    String pid = gjContenthigh.getPid();         //父编号
		    String projectPrefs = gjContenthigh.getProjectPrefs();         //项目描述
		    String checkContent = gjContenthigh.getCheckContent();         //检查内容
		    String checkStand = gjContenthigh.getCheckStand();         //检查标准
		    String checkMain = gjContenthigh.getCheckMain();         //评分要点
		    String inScores = gjContenthigh.getInScores();         //分值
		    String oldId = gjContenthigh.getOldId();          //旧的编号，用于更新
		    String hql = "update score.gj_contenthigh set id =?,pid=?,project_prefs=?,check_content=?,check_stand=?,check_main=?,in_scores=? where id=?";
		    this.getDao().saveOrUpdateBySql(hql, new Object[] {id,pid,projectPrefs,checkContent,checkStand,checkMain,inScores,oldId});
		    String hql2 = "update score.gj_contenthigh set pid=? where pid=?";
			this.getDao().saveOrUpdateBySql(hql2, new Object[] {id,oldId});
		} catch (Exception e) {
			e.printStackTrace();
			return new Message(Message.ERROR, "更新评分标准失败").toJson();
		}
		return new Message(Message.OK, gjContenthigh.getId()).toJson();
	}

    /**
     * @Description 删除记录
     * @author 张立增
     * @Date 2020年2月28日 下午8:04:22
     */
	public String deleteByIds(String ids) {
		String[] idArr = ids.split(",");
		int lens = idArr.length;
		try {
			for (int i = 0; i < lens; i++) {
				GjContenthigh gjContenthigh = gjContenthighRepository.findOne(idArr[i]);
				gjContenthighRepository.delete(gjContenthigh);
			}
		} catch (Exception e) {
			return new Message(Message.ERROR, "删除记录失败").toJson();
		}
		return new Message(Message.OK, lens + "").toJson();
	}

    /**
	 * @Description 得到数列表
	 * @author 张立增
	 * @Date 2020年2月26日 下午8:22:12
	 */
	public String getTreeGridData() {
		try {
			String hql = "select id,pid,CONCAT(id,' ',project_prefs) AS project_prefs from score.gj_contenthigh order by id,pid ";
			List<GjContenthigh> list = this.getDao().findBySql(hql,GjContenthigh.class);
			return JsonUtils.dealJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Description 得到列表数据
	 * @author 张立增
	 * @Date 2020年2月26日 下午9:44:26
	 */
	public Page<GjContenthigh> getDataGridByPid(String pid, Pageable pageable) {
		try {
			String hql = "select id,CASE WHEN LENGTH(project_prefs)=0 THEN (SELECT project_prefs FROM  score.gj_contenthigh WHERE id =? LIMIT 0,1) ELSE project_prefs END AS project_prefs,in_scores from score.gj_contenthigh where pid = ?";
			return super.getDao().findEntityPagingBySql(hql, new Object[] {pid,pid}, pageable, GjContenthigh.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Description 得到一个编号的详细信息
	 * @author 张立增
	 * @Date 2020年2月29日 下午6:20:46
	 */
	public String getOneDetailData(String id, String pid) {
		try {
			String hql = "SELECT CONCAT(id,' ',project_prefs) AS project_prefs,check_content,check_stand,check_main,in_scores FROM  " + 
					"(SELECT CASE WHEN LENGTH(project_prefs)=0 THEN pid ELSE id END AS id, " + 
					"CASE WHEN LENGTH(project_prefs)=0 THEN (SELECT project_prefs FROM  score.gj_contenthigh WHERE id =? LIMIT 0,1) ELSE project_prefs END AS project_prefs, " + 
					"check_content,check_stand,check_main,in_scores FROM score.gj_contenthigh WHERE id = ?)a";
			List<GjContenthigh> list = this.getDao().findBySql(hql,new Object[] {pid,id},GjContenthigh.class);
			return JsonUtils.dealJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

